﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Strategia.Input
{
    public class Gui : DrawableGameComponent
    {
        GraphicsDevice graphicsDevice;

        Vector2 center, max;

        MouseState newState, oldState;

        SpriteBatch spriteBatch;

        Texture2D left;
        Texture2D dleft;
        Texture2D down;
        Texture2D right;
        Texture2D dright;
        Texture2D up;
        Texture2D num1;
        Texture2D num4;
        Texture2D num5;
        Texture2D p;
        Texture2D ent;
        Texture2D st;
        Texture2D home;
        Texture2D end;

        SpriteFont font;
        SpriteFont font_skill;
        ContentManager content;

        public string Info1 = "";
        public string Info2 = "";
        public string Info3 = "Zmiana polozenia/rozmiaru";

        public Keys leftKey, dleftKey, downKey, rightKey, drightKey, upKey, num1Key, num4Key, num5Key, pKey, entKey, stKey, homeKey, endKey;
        public Vector2 leftPos, dleftPos, downPos, rightPos, drightPos, upPos, num1Pos, num4Pos, num5Pos, pPos, entPos, stPos, homePos, endPos, size;
        
        public float pozX = 0.1f, pozY = 0, pozZ = 0;
        public float W = 0, L = 0, H = 0;

        public bool zmiana = false;

        public Gui(ContentManager content, SpriteBatch sprite, GraphicsDevice graphicsDevice, Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            this.graphicsDevice = graphicsDevice;
            this.max.X = graphicsDevice.PresentationParameters.BackBufferWidth;
            this.max.Y = graphicsDevice.PresentationParameters.BackBufferHeight;
            this.center.X = this.max.X / 2;
            this.center.Y = this.max.Y / 2;
            this.content = content;
            this.spriteBatch = sprite;
            
            //Left
            this.left = content.Load<Texture2D>("GUI/left");
            leftKey = Keys.Left;
            //dLeft
            this.dleft = content.Load<Texture2D>("GUI/dleft");
            dleftKey = Keys.OemComma;
            //Down
            this.down = content.Load<Texture2D>("GUI/down");
            downKey = Keys.Down;
            //Right
            this.right = content.Load<Texture2D>("GUI/right");
            rightKey = Keys.Right;
            //dRight
            this.dright = content.Load<Texture2D>("GUI/dright");
            drightKey = Keys.OemPeriod;
            //Up
            this.up = content.Load<Texture2D>("GUI/up");
            upKey = Keys.Up;
            //num1
            this.num1 = content.Load<Texture2D>("GUI/num1");
            num1Key = Keys.NumPad1;
            //num4
            this.num4 = content.Load<Texture2D>("GUI/num4");
            num4Key = Keys.NumPad4;
            //num5
            this.num5 = content.Load<Texture2D>("GUI/num5");
            num5Key = Keys.NumPad5;
            //p
            this.p = content.Load<Texture2D>("GUI/p");
            pKey = Keys.P;
            //ent
            this.ent = content.Load<Texture2D>("GUI/ent");
            entKey = Keys.Enter;
            //st
            this.st = content.Load<Texture2D>("GUI/u");
            stKey = Keys.U;
            //home
            this.home = content.Load<Texture2D>("GUI/home");
            homeKey = Keys.Home;
            //end
            this.end = content.Load<Texture2D>("GUI/end");
            endKey = Keys.End;

            //Font
            this.font = content.Load<SpriteFont>("Font/Font");
            this.font_skill = content.Load<SpriteFont>("Font/skill_font");

            this.leftPos = new Vector2((int)(center.X - 125) - 300, (int)(max.Y - 75));
            this.dleftPos = new Vector2((int)(center.X - 200) - 300, (int)(max.Y - 75));
            this.downPos = new Vector2((int)(center.X - 25) - 300, (int)(max.Y - 75));
            this.rightPos = new Vector2((int)(center.X + 75) - 300, (int)(max.Y - 75));
            this.drightPos = new Vector2((int)(center.X + 150) - 300, (int)(max.Y - 75));
            this.upPos = new Vector2((int)(center.X - 25) - 300, (int)(max.Y - 175));
            this.endPos = new Vector2((int)(center.X + 75) - 300, (int)(max.Y - 175));
            this.homePos = new Vector2((int)(center.X - 125) - 300, (int)(max.Y - 175));
            this.num1Pos = new Vector2((int)(center.X + 200 + 40) - 300, (int)(max.Y - 75));
            this.num4Pos = new Vector2((int)(center.X + 200 + 40) - 300, (int)(max.Y - 175));
            this.num5Pos = new Vector2((int)(center.X + 325) - 100, (int)(max.Y - 75));
            this.pPos = new Vector2((int)(center.X + 450), (int)(max.Y - 75));
            this.entPos = new Vector2((int)(max.X - 75), (int)(25));
            this.stPos = new Vector2((int)(max.X - 75), (int)(150));
            this.size = new Vector2(50);
        }

        protected override void LoadContent() { }

        public override void Initialize() { }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null);
            
            spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null);

            
            spriteBatch.Draw(left, new Rectangle((int)leftPos.X, (int)leftPos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, leftKey.ToString(), new Vector2(leftPos.X+15f, leftPos.Y+50f), Color.White);

            spriteBatch.Draw(right, new Rectangle((int)rightPos.X, (int)rightPos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, rightKey.ToString(), new Vector2(rightPos.X + 15f, rightPos.Y + 50f), Color.White);

            spriteBatch.Draw(dleft, new Rectangle((int)dleftPos.X, (int)dleftPos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, ",", new Vector2(dleftPos.X + 15f, dleftPos.Y + 50f), Color.White);

            spriteBatch.Draw(dright, new Rectangle((int)drightPos.X, (int)drightPos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, ".", new Vector2(drightPos.X + 15f, drightPos.Y + 50f), Color.White);


            spriteBatch.Draw(down, new Rectangle((int)downPos.X, (int)downPos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, downKey.ToString(), new Vector2(downPos.X + 15f, downPos.Y + 50f), Color.White);

            spriteBatch.Draw(up, new Rectangle((int)upPos.X, (int)upPos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, upKey.ToString(), new Vector2(upPos.X + 15f, upPos.Y + 50f), Color.White);

            if (zmiana)
            {
                spriteBatch.Draw(home, new Rectangle((int)homePos.X, (int)homePos.Y, 50, 50), new Color(255, 255, 255, 255));
                spriteBatch.DrawString(font, homeKey.ToString(), new Vector2(homePos.X + 15f, homePos.Y + 50f), Color.White);

                spriteBatch.Draw(end, new Rectangle((int)endPos.X, (int)endPos.Y, 50, 50), new Color(255, 255, 255, 255));
                spriteBatch.DrawString(font, endKey.ToString(), new Vector2(endPos.X + 15f, endPos.Y + 50f), Color.White);
            }

            spriteBatch.Draw(num1, new Rectangle((int)num1Pos.X, (int)num1Pos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, "Num1", new Vector2(num1Pos.X + 5f, num1Pos.Y + 50f), Color.White);

            spriteBatch.Draw(num4, new Rectangle((int)num4Pos.X, (int)num4Pos.Y, 50, 50), new Color(255, 255, 255, 255));
            spriteBatch.DrawString(font, "Num4", new Vector2(num4Pos.X + 5f, num4Pos.Y + 50f), Color.White);

            if (!zmiana)
            {
                spriteBatch.Draw(num5, new Rectangle((int)num5Pos.X, (int)num5Pos.Y, 50, 50), new Color(255, 255, 255, 255));
                spriteBatch.DrawString(font, "Num5", new Vector2(num5Pos.X + 5f, num5Pos.Y + 50f), Color.White);
                spriteBatch.DrawString(font, "Przelacz na polozenie/rozmiar", new Vector2(num5Pos.X - 50f, num5Pos.Y - 50f), Color.White);

                spriteBatch.Draw(p, new Rectangle((int)pPos.X, (int)pPos.Y, 50, 50), new Color(255, 255, 255, 255));
                spriteBatch.DrawString(font, pKey.ToString(), new Vector2(pPos.X + 5f, pPos.Y + 50f), Color.White);
                spriteBatch.DrawString(font, "Dodaj obiekt", new Vector2(pPos.X + 5f, pPos.Y - 50f), Color.White);

                spriteBatch.Draw(ent, new Rectangle((int)entPos.X, (int)entPos.Y, 50, 50), new Color(255, 255, 255, 255));
                spriteBatch.DrawString(font, entKey.ToString(), new Vector2(entPos.X + 5f, entPos.Y + 50f), Color.White);
                spriteBatch.DrawString(font, "Dalej", new Vector2(entPos.X + 5f, entPos.Y + 65f), Color.White);

                spriteBatch.Draw(st, new Rectangle((int)stPos.X, (int)stPos.Y, 50, 50), new Color(255, 255, 255, 255));
                spriteBatch.DrawString(font, stKey.ToString(), new Vector2(stPos.X + 15f, stPos.Y + 50f), Color.White);
                spriteBatch.DrawString(font, "Stale cieplo?", new Vector2(stPos.X - 15f, stPos.Y + 65f), Color.White);
            }
            spriteBatch.DrawString(font, Info3, new Vector2(upPos.X - 50f, upPos.Y - 50f), Color.White);

            spriteBatch.End();

            //Antoher batches for alpha mode
            
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null);
            
            spriteBatch.DrawString(font, Info1, new Vector2(center.X + 400, center.Y), Color.White);
            spriteBatch.DrawString(font, Info2, new Vector2(center.X + 400, center.Y-50), Color.White);
            //spriteBatch.DrawString(font, Mouse.GetState().ToString(), new Vector2(graphicsDevice.PresentationParameters.BackBufferWidth / 2, 82), Color.White);
            if (!zmiana)
            {
                spriteBatch.DrawString(font, "Pozycja (x,z)", new Vector2(10, 82), Color.Red);
                spriteBatch.DrawString(font, "Rozmiar (w,h,l)", new Vector2(10, 104), Color.Yellow);


                //Hp values
                spriteBatch.DrawString(font, (Math.Round(pozX, 1)).ToString() + " x " + (Math.Round(pozY, 1)).ToString() + " x " + (Math.Round(pozZ, 1)).ToString() + " [m]", new Vector2(100, 85), Color.White);
                //Energy values
                spriteBatch.DrawString(font, (Math.Round(W, 1)).ToString() + " x " + (Math.Round(H, 1)).ToString() + " x " + (Math.Round(L, 1)).ToString() + " [m]", new Vector2(100, 107), Color.White);
            }
            spriteBatch.End();
        }

        int c = 0;
        public override void Update(GameTime gameTime)
        {
        }

        public int Clicked(MouseState actualState, MouseState oldState)
        {
            // L1, D2, R3, U4, Num1=5, Num4=6, Num5=7
            newState = actualState;
            this.oldState = oldState;

            if (newState.LeftButton == ButtonState.Pressed)
            {
                //skill1
                if (newState.X >= leftPos.X && newState.Y >= leftPos.Y && newState.X <= (leftPos.X + size.X) && newState.Y <= (leftPos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("Lewo");
                    return 1;
                    c++;
                }
                //skill3
                if (newState.X >= rightPos.X && newState.Y >= rightPos.Y && newState.X <= (rightPos.X + size.X) && newState.Y <= (rightPos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("Prawo");
                    return 3;
                    c++;
                }
                if (newState.X >= dleftPos.X && newState.Y >= dleftPos.Y && newState.X <= (dleftPos.X + size.X) && newState.Y <= (dleftPos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("Lewo");
                    return 11;
                    c++;
                }
                //skill3
                if (newState.X >= drightPos.X && newState.Y >= drightPos.Y && newState.X <= (drightPos.X + size.X) && newState.Y <= (drightPos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("Prawo");
                    return 12;
                    c++;
                }
                //skill2
                if (newState.X >= downPos.X && newState.Y >= downPos.Y && newState.X <= (downPos.X + size.X) && newState.Y <= (downPos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("Dol");
                    return 2;
                    c++;
                }
                if (newState.X >= upPos.X && newState.Y >= upPos.Y && newState.X <= (upPos.X + size.X) && newState.Y <= (upPos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("Gora");
                    return 4;
                    c++;
                }
                if (newState.X >= num1Pos.X && newState.Y >= num1Pos.Y && newState.X <= (num1Pos.X + size.X) && newState.Y <= (num1Pos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("NUM1");
                    return 5;
                    c++;
                }
                if (newState.X >= num4Pos.X && newState.Y >= num4Pos.Y && newState.X <= (num4Pos.X + size.X) && newState.Y <= (num4Pos.Y + size.Y))
                {
                    if (c == 0) Console.WriteLine("NUM4");
                    return 6;
                    c++;
                }
            }
            if (newState.LeftButton == ButtonState.Released && oldState.LeftButton == ButtonState.Pressed)
            {
                if (!zmiana)
                {
                    if (oldState.X >= num5Pos.X && oldState.Y >= num5Pos.Y && oldState.X <= (num5Pos.X + size.X) && oldState.Y <= (num5Pos.Y + size.Y))
                    {
                        Console.WriteLine("NUM5");
                        return 7;
                    }
                    if (oldState.X >= pPos.X && oldState.Y >= pPos.Y && oldState.X <= (pPos.X + size.X) && oldState.Y <= (pPos.Y + size.Y))
                    {
                        Console.WriteLine("P");
                        return 8;
                    }
                    if (oldState.X >= entPos.X && oldState.Y >= entPos.Y && oldState.X <= (entPos.X + size.X) && oldState.Y <= (entPos.Y + size.Y))
                    {
                        Console.WriteLine("Enter");
                        return 9;
                    }
                    if (oldState.X >= stPos.X && oldState.Y >= stPos.Y && oldState.X <= (stPos.X + size.X) && oldState.Y <= (stPos.Y + size.Y))
                    {
                        Console.WriteLine("U");
                        return 10;
                    }
                }
                if (zmiana)
                {
                    if (oldState.X >= homePos.X && oldState.Y >= homePos.Y && oldState.X <= (homePos.X + size.X) && oldState.Y <= (homePos.Y + size.Y))
                    {
                        Console.WriteLine("Home");
                        return 13;
                    }
                    if (oldState.X >= endPos.X && oldState.Y >= endPos.Y && oldState.X <= (endPos.X + size.X) && oldState.Y <= (endPos.Y + size.Y))
                    {
                        Console.WriteLine("End");
                        return 14;
                    }
                }
            }

            return 0;

        }
    }
}
